package com.hifar.test.api.model;

import com.hifar.test.api.constant.OrganizationProcessingErrorCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织信息处理结果类
 * 封装组织信息数据处理的详细结果
 */
public class OrganizationProcessingResult {
    
    private boolean success;
    private List<OrganizationProcessingErrorCode.OrganizationProcessingErrorResponse> errors;
    private int totalCount;
    private int successCount;
    private int errorCount;
    private long processingTime;
    private String operation;
    
    public OrganizationProcessingResult() {
        this.success = true;
        this.errors = new ArrayList<>();
        this.totalCount = 0;
        this.successCount = 0;
        this.errorCount = 0;
        this.processingTime = 0;
    }
    
    public OrganizationProcessingResult(boolean success) {
        this();
        this.success = success;
    }
    
    /**
     * 添加错误
     */
    public void addError(OrganizationProcessingErrorCode errorCode, String operation, String dataId) {
        this.errors.add(errorCode.createErrorResponse(operation, dataId));
        this.errorCount++;
        this.success = false;
    }
    
    /**
     * 添加带异常的错误
     */
    public void addError(OrganizationProcessingErrorCode errorCode, String operation, String dataId, Exception exception) {
        this.errors.add(errorCode.createErrorResponse(operation, dataId, exception));
        this.errorCount++;
        this.success = false;
    }
    
    /**
     * 添加错误响应
     */
    public void addError(OrganizationProcessingErrorCode.OrganizationProcessingErrorResponse errorResponse) {
        this.errors.add(errorResponse);
        this.errorCount++;
        this.success = false;
    }
    
    /**
     * 标记处理成功
     */
    public void markSuccess() {
        this.successCount++;
    }
    
    /**
     * 设置总数
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    
    /**
     * 设置处理时间
     */
    public void setProcessingTime(long processingTime) {
        this.processingTime = processingTime;
    }
    
    /**
     * 设置操作类型
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }
    
    /**
     * 检查是否成功
     */
    public boolean isSuccess() {
        return this.success && this.errorCount == 0;
    }
    
    /**
     * 获取错误数量
     */
    public int getErrorCount() {
        return this.errorCount;
    }
    
    /**
     * 获取成功数量
     */
    public int getSuccessCount() {
        return this.successCount;
    }
    
    /**
     * 获取总数
     */
    public int getTotalCount() {
        return this.totalCount;
    }
    
    /**
     * 获取处理时间
     */
    public long getProcessingTime() {
        return this.processingTime;
    }
    
    /**
     * 获取操作类型
     */
    public String getOperation() {
        return this.operation;
    }
    
    /**
     * 获取错误列表
     */
    public List<OrganizationProcessingErrorCode.OrganizationProcessingErrorResponse> getErrors() {
        return this.errors;
    }
    
    /**
     * 获取成功率
     */
    public double getSuccessRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) successCount / totalCount * 100;
    }
    
    /**
     * 获取失败率
     */
    public double getFailureRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) errorCount / totalCount * 100;
    }
    
    /**
     * 检查是否有错误
     */
    public boolean hasErrors() {
        return !errors.isEmpty();
    }
    
    /**
     * 获取处理摘要
     */
    public String getProcessingSummary() {
        StringBuilder summary = new StringBuilder();
        summary.append("处理完成，总数: ").append(totalCount);
        summary.append(", 成功: ").append(successCount);
        summary.append(", 失败: ").append(errorCount);
        summary.append(", 成功率: ").append(String.format("%.2f", getSuccessRate())).append("%");
        summary.append(", 处理时间: ").append(processingTime).append("ms");
        return summary.toString();
    }
    
    /**
     * 获取错误摘要
     */
    public String getErrorSummary() {
        if (errors.isEmpty()) {
            return "处理成功，无错误";
        }
        
        StringBuilder summary = new StringBuilder();
        summary.append("处理失败，共发现 ").append(errorCount).append(" 个错误：\n");
        
        for (int i = 0; i < errors.size(); i++) {
            OrganizationProcessingErrorCode.OrganizationProcessingErrorResponse error = errors.get(i);
            summary.append(i + 1).append(". ").append(error.getMessage());
            if (error.getOperation() != null) {
                summary.append(" (操作: ").append(error.getOperation()).append(")");
            }
            if (error.getDataId() != null) {
                summary.append(" (数据ID: ").append(error.getDataId()).append(")");
            }
            summary.append("\n");
        }
        
        return summary.toString();
    }
    
    @Override
    public String toString() {
        return "OrganizationProcessingResult{" +
                "success=" + success +
                ", totalCount=" + totalCount +
                ", successCount=" + successCount +
                ", errorCount=" + errorCount +
                ", processingTime=" + processingTime + "ms" +
                ", successRate=" + String.format("%.2f", getSuccessRate()) + "%" +
                ", hasErrors=" + hasErrors() +
                '}';
    }
}
